<?php
// +----------------------------------------------------------------------
// | @projectName  【order_system---点餐系统】
// +----------------------------------------------------------------------
// | @author        山西创客空间科技有限公司
// +----------------------------------------------------------------------
// | @date          2017年10月13日 星期五
// +----------------------------------------------------------------------
// | @Copyright     http://sx-ck.com All rights reserved.      
// +----------------------------------------------------------------------
namespace app\common;

use think\Controller;
use app\admin\model\Common;
use think\Db;
use think\Request;
use think\Env;
use think\Session;
class Base extends Controller 
{

    public $request;
    // 初始化操作,并且进行登陆操作的验证与权限把控
    public function _initialize ()
    {
        $request = Request::instance();
		define('CONTROL',$request->controller());
		define('MODULE',$request->module());
		define('IP',$request->ip());
        define('ACTION',$request->action());

        //是否拦截手机--根据header头部信息判断是否进行登陆拦截
        $user_agent = getallheaders()['User-Agent'];
        
        if (!is_string($user_agent) && Common::check_empty($user_agent) === true) {
            return Base::echo_error(Error::HEADER_INFO_ERROR);
        }
        $user_agent = substr($user_agent,0,6);
        if ($user_agent !== 'okhttp') {
            if (Common::check_empty(Session::get('tmp_login')) === true) {
                $this -> redirect('Login/index');
                // echo "<script>window.location.href = '/order_system/Login/index'</script>";
            }
        }
        

    }

    // 数据处理成功调用的api接口
    public static function echo_success($message = '') 
    {
        $row = [
            "code"  => 0,
            "message"   => $message
        ];
        return json_encode($row,JSON_UNESCAPED_UNICODE);
    }

    // 数据处理失败调用的api接口
    public static function echo_error($message = '') 
    {
        $row = [
            "code"  => -1,
            "message"   => $message
        ];
        return json_encode($row,JSON_UNESCAPED_UNICODE);
    }

    // 连接mongodb数据库
    public static function connect_mongodb($dbname) 
    {
        $m = new \MongoClient (Env::get('mongo_db'));
        return $m -> selectDB($dbname);
    }

    // 自动生成guid的方法
    protected function guid() 
    {
        if (function_exists('com_create_guid')){
            $uuid = com_create_guid();
            $uuid1 = preg_match('/^{(.+)}$/', $uuid, $match);
            return $match[1];
        }else{
            mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
            $charid = strtoupper(md5(uniqid(rand(), true)));
            $hyphen = chr(45);
            $uuid = substr($charid, 0, 8).$hyphen
                    .substr($charid, 8, 4).$hyphen
                    .substr($charid,12, 4).$hyphen
                    .substr($charid,16, 4).$hyphen
                    .substr($charid,20,12);
            return $uuid;
        }
    }

    // 封装根据某个字段进行排序的方法 $arr接收的数组,$field根据排序的字段,$sort为排序的规则
    public function f_order_sort ($arr,$field,$sort) 
    {
        $order = [];
        foreach ($arr as $key => $value) {
            $order[] = $value[$field];
        }
        if ($sort == 1) {
            array_multisort($order,SORT_ASC,$arr);
            $arr_count = count($arr);                        
            foreach ($arr as $key => &$val) {
                $val['sort_name'] = '第'.($arr_count-$key).'名';
            }
        } else {
            array_multisort($order,SORT_DESC,$arr);
            foreach ($arr as $key => &$val) {
                $val['sort_name'] = '第'.($key+1).'名';
            }
        }
        
        return $arr;
    }
}